package com.example.service.impl;

import com.example.dao.EmploymentDao;
import com.example.domain.Department;
import com.example.domain.Employment;
import com.example.domain.ExcelData;
import com.example.service.EmploymentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author luo
 */
@Service
public class EmploymentServiceImpl implements EmploymentService {

    @Autowired
    private EmploymentDao employmentDao;

    @Override
    public List<Employment> selectAllEmploy() {
        return employmentDao.selectAllEmploy();
    }

    @Override
    public Employment selectEmployById(Integer id) {
        return employmentDao.selectEmployById(id);
    }

    @Override
    public List<Employment> selectEmploymentByName(Employment employment){
        return employmentDao.selectEmploymentByName(employment);
    }

    @Override
    public int addEmployment(Employment employment) {
        return employmentDao.addEmployment(employment);
    }

    @Override
    public int updateEmployment(Employment employment) {
        return employmentDao.updateEmployment(employment);
    }

    @Override
    public int deleteEmployment(Integer id) {
        return employmentDao.deleteEmployment(id);
    }

    @Override
    public PageInfo<Employment> findPage(int page, int size) {
        // 开启分页插件,放在查询语句上面 帮助生成分页语句
        PageHelper.startPage(page, size); //底层实现原理采用改写语句   将下面的方法中的sql语句获取到然后做个拼接 limit AOP技术
        List<Employment> listUser = employmentDao.selectAllEmploy();
        // 封装分页之后的数据  返回给客户端展示  PageInfo做了一些封装 作为一个类
        PageInfo<Employment> pageInfoUser = new PageInfo<Employment>(listUser);
        return pageInfoUser;
    }
    /**
     * 结果的导出
     */
    @Override
    public ExcelData getOutputExcelProjectData() {


        //Excel实体类
        ExcelData excelData = new ExcelData();

        //找到表中需要添加的数据
        List<Employment> list = employmentDao.selectAllEmploy();

        //设置sheet名称
        excelData.setName("招聘信息");

        //设置表头字段
        List<String> titles = new ArrayList<String>();
        titles.add("Id");
        titles.add("职位");
        titles.add("薪资");
        titles.add("人数");
        titles.add("工作内容");
        titles.add("联系电话");
        titles.add("要求经验");
        excelData.setTitles(titles);

        //把expertUserList转换为List<List<Object>> rows
        List<List<Object>> rows = new ArrayList<List<Object>>();

        //加入数据
        for (Employment employment : list) {
            List<Object> row = new ArrayList<Object>();
            row.add(employment.getId());
            row.add(employment.getPosition());
            row.add(employment.getSalary());
            row.add(employment.getNum());
            row.add(employment.getContent());
            row.add(employment.getPhone());
            row.add(employment.getYear());
            rows.add(row);
        }

        //设置要输出的记录
        excelData.setRows(rows);
        return excelData;
    }
}
